//
//  VoiceCell.swift
//  YueChat
//
//  Created by IMac on 2024/9/23.
//

import UIKit

class VoiceCell: BaseChatCell {

    @IBOutlet weak var voiceImg: UIImageView!
    
    @IBOutlet weak var timeLab: UILabel!
    
    @IBOutlet weak var voice_read: UIImageView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        self.msgView.backgroundColor = UIColor.clear
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func baseCellHeight(message: DMCCMessage, chatType: Bool, mulSelect: Bool, isMySend: Bool) {
        if let soundContent = message.content as? OsnVoiceMessageContent {
            message.msgCellHeight = 55
            message.msgCellWidth = 80 + (soundContent.duration / 60)
        }
    }
    
    override func setMessage(_ message: DMCCMessage) {
        super.setMessage(message)
        
        if let soundContent = message.content as? OsnVoiceMessageContent {
            self.timeLab.text = "\(soundContent.duration)s"
//            self.voiceViewW.constant = 80 + (soundContent.duration / 60)
//            self.transLabel.text = message.extra
            
            if message.direction == OsnMessageDirection.Receive {
                if message.status == OsnMessageStatus.Unread {
                    self.voice_read.isHidden = false
                } else {
                    self.voice_read.isHidden = true
                }
            }
            
            self.setVoiceAnimationImg(message)
            
//            if let fromUserId = message.getFrom(), fromUserId == self.myInfo.userId {
//                self.msgTimeLabel.textAlignment = .right
//                
//               
//            } else {
//               
//            }

        }
    }

    func setVoiceAnimationImg(_ message: DMCCMessage) {
        var images: [UIImage] = []
        
        if message.getFrom() == self.myInfo.userId {
            for i in 1..<4 {
                let path = "voice_r_0\(i)"
                let image = UIImage(named: path)
                images.append(image!)
            }
        } else {
            for i in 1..<4 {
                let path = "voice_l_0\(i)"
                let image = UIImage(named: path)
                images.append(image!)
            }
        }
        
        self.voiceImg.animationImages = images
        self.voiceImg.animationDuration = 1
    }
    
}
